package com.SoftwareManage.controller;

import com.SoftwareManage.entity.Software;
import com.SoftwareManage.service.SoftwareService;
import com.SoftwareManage.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/software")
public class SoftwareController {
    @Autowired
    private SoftwareService softwareService;

    private final static Logger logger = LoggerFactory.getLogger(SoftwareController.class);

    //跳转到software页面
    @RequestMapping("/software")
    public String toSoftware(Model model,
                             HttpServletRequest request){
        String ipAddr= IpUtil.getIpAddr(request);
        logger.info(ipAddr+"------跳转到软件信息页面------");
        List<Software> softwares=softwareService.selectAll();
        model.addAttribute("number",softwares.size());
        return "software";
    }


    //software页面list回调函数请求所有数据
    @RequestMapping(value = "/lists")
    @ResponseBody
    public Map<String, Object> showEntity(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) throws IOException {
        List<Software> entities = softwareService.selectAll();
        int pageNum = (page - 1) * limit;
        List<Software> entity = softwareService.selectAlls(pageNum, limit);

        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", entities.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", entity);
        //返回给前端
        return tableData;
    }

    //查询信息
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> search(
            @RequestParam(required = false) String software,
            @RequestParam(required = false) String person,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        List<Software> entities=softwareService.search(software,person);
        int pageNum = (page - 1) * limit;
        List<Software> entity = softwareService.searchs(software,person,pageNum, limit);
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", entities.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", entity);
        //返回给前端
        return tableData;
    }

    //	修改信息
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editEntity(
            @RequestParam("id") int id,
            @RequestParam("software") String software,
            @RequestParam("person") String person,
            @RequestParam("ip") String ip,
            @RequestParam("tel") String tel,
            @RequestParam("remark") String remark,
            HttpServletRequest request){
        String ipAddr= IpUtil.getIpAddr(request);
        Map<String, Object> tableData = new HashMap<String, Object>();
        softwareService.update(id, software, person, ip, tel,remark);
        tableData.put("code", 100);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        logger.info(ipAddr+"------[software]修改成功------");
        //返回给前端
        return tableData;
    }

    //	添加信息
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addEntity(
            @RequestParam("a_software") String software,
            @RequestParam("a_person") String person,
            @RequestParam("a_ip") String ip,
            @RequestParam("a_tel") String tel,
            @RequestParam("a_remark") String remark,
            HttpServletRequest request){
        String ipAddr= IpUtil.getIpAddr(request);
        Map<String, Object> tableData = new HashMap<String, Object>();
        List<String> softwares=softwareService.selectAllSoftwares();
        if (softwares.contains(software)){
            tableData.put("code", 300);
            tableData.put("msg", "软件名已存在！");
            //将全部数据的条数作为count传给前台（一共多少条）
            tableData.put("count", 0);
            //将分页后的数据返回（每页要显示的数据）
            tableData.put("data", "");
            logger.info(ipAddr+"------[software]添加失败------");
            //返回给前端
            return tableData;
        }
        softwareService.add(software,person,ip,tel,remark);
        tableData.put("code", 200);
        tableData.put("msg", "添加成功！");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        logger.info(ipAddr+"------[software]添加成功------");
        //返回给前端
        return tableData;
    }

    //删除信息
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> removeEntity(@RequestBody int[] ids,
                                            HttpServletRequest request) {
        String ipAddr= IpUtil.getIpAddr(request);
        Map<String, Object> tableData = new HashMap<String, Object>();
        for (int id : ids) {
            softwareService.delete(id);
        }
        tableData.put("code", 300);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        logger.info(ipAddr+"------[software]删除成功------");
        //返回给前端
        return tableData;
    }

}
